<template>
  <div>
    <vxe-switch v-model="tableTooltipConfig.showAll" open-label="显示提示" close-label="不显示提示"></vxe-switch>
    <vxe-toolbar>
      <template #buttons>
        <vxe-button @click="allAlign = 'left'">居左</vxe-button>
        <vxe-button @click="allAlign = 'center'">居中</vxe-button>
        <vxe-button @click="allAlign = 'right'">居右</vxe-button>
      </template>
    </vxe-toolbar>

    <vxe-table ref="xTbl" size="mini" :data="tableData" :align="allAlign" border :tooltip-config="tableTooltipConfig" @checkbox-change="selectChangeEvent1">
      <vxe-column type="seq" width="60"></vxe-column>
      <vxe-column field="name" title="Name">
        <template #default="{ row }">
          <span>自定义插槽模板 {{ row.name }}</span>
        </template>
      </vxe-column>
      <vxe-column field="sex" title="Sex"></vxe-column>
      <vxe-column field="age" title="Age"></vxe-column>
    </vxe-table>

    <!-- strict: false 允许取消
    highlight: true 高亮选中行
    trigger: 'row' 行选择模式 -->
    <vxe-grid
      v-bind="gridOptions"
      ref="xTblGrid"
      :radio-config="{ strict: false, highlight: true, isCurrent: true, isHover: true, trigger: 'row' }"
      :checkbox-config="{ checkField: 'checked', trigger: 'row' }"
      @checkbox-all="selectAllChangeEvent1"
    >
      <template #name="{ row }">
        <span>自定义插槽模板 {{ row.name }}</span>
      </template>
    </vxe-grid>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { VxeGridProps, VxeTablePropTypes } from 'vxe-table'

const allAlign = ref('left')
const tableTooltipConfig = ref<VxeTablePropTypes.TooltipConfig>({
  showAll: true,
  enterable: true,
})

interface UserVO {
  id: number
  name: string
  role: string
  sex: string
  age: number
  address: string
}

const tableData = ref<UserVO[]>([
  { id: 10001, name: 'Test1', role: 'Develop', sex: 'Man', age: 28, address: 'test abc' },
  { id: 10002, name: 'Test2', role: 'Test', sex: 'Women', age: 22, address: 'Guangzhou' },
  { id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, address: 'Shanghai' },
  { id: 10004, name: 'Test4', role: 'Designer', sex: 'Women', age: 24, address: 'Shanghai' },
])

const gridOptions = reactive<VxeGridProps<UserVO>>({
  border: true,
  columns: [
    { type: 'seq', width: 50 },
    // 单选
    { field: '', title: '单选', type: 'radio', width: 50 },
    // 复选
    { field: '', title: '复选', type: 'checkbox', width: 50 },
    { field: 'name', title: 'Name', slots: { default: 'name' } },
    { field: 'sex', title: 'Sex', showHeaderOverflow: true },
    { field: 'address', title: 'Address', showOverflow: true },
  ],
  data: [
    { id: 10001, name: 'Test1', role: 'Develop', sex: 'Man', age: 28, address: 'test abc' },
    { id: 10002, name: 'Test2', role: 'Test', sex: 'Women', age: 22, address: 'Guangzhou' },
    { id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, address: 'Shanghai' },
    { id: 10004, name: 'Test4', role: 'Designer', sex: 'Women', age: 24, address: 'Shanghai' },
  ],
})
const xTblGrid = ref<VxeGrid>()
const xTbl = ref<VxeTable>()
const selectAllChangeEvent1: VxeTableEvents.CheckboxAll = checked => {
  const tbl = xTblGrid.value
  const records = tbl.getCheckboxRecords()
  console.log(checked ? '所有勾选事件' : '所有取消事件', records)
}
const selectChangeEvent1: VxeTableEvents.CheckboxChange = checked => {
  const tbl = xTbl.value
  const records = tbl.getCheckboxRecords()
  console.log(checked ? '勾选事件' : '取消事件', records)
}
</script>

<style scoped></style>
